package cn.jinjimi.base.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

/**
 * Created by Roy
 * Date: 16/1/12
 */
public class ClipBoardUtils {
    public static ClipboardManager mClipboardManager;

    public static ClipboardManager getClipboardManagerInstance(Context context) {
        if (mClipboardManager == null) {
            mClipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        }
        return mClipboardManager;
    }

    /* 实现复制的功能 */
    public static void copy(Context context, String content) {
        ClipboardManager mClipBoard = getClipboardManagerInstance(context);
        ClipData clip = ClipData.newPlainText("copy", content);
        mClipBoard.setPrimaryClip(clip);
    }

    /* 实现粘贴的功能 */
    public static String paste(Context context) {
        ClipboardManager mClipBoard = getClipboardManagerInstance(context);

        String resultString = "";
        // 检查剪贴板是否有内容
        if (!mClipBoard.hasPrimaryClip()) {
            //此时剪贴板为空
        } else {
            ClipData clipData = mClipBoard.getPrimaryClip();
            int count = clipData.getItemCount();

            for (int i = 0; i < count; ++i) {
                ClipData.Item item = clipData.getItemAt(i);
                CharSequence str = item
                        .coerceToText(context);
                resultString += str;
            }

        }
        return resultString;
    }
}
